import bge

# This script runs only once, in the beginning of the game start. It will read the initial database
# coordinates, and add objects accordingly. This code is the same as the code in 'update.py', but runs
# only once. 
#
# If you have global game variables, initiate them here
#

bge.selModel = "C1"
bge.changeModel = None
bge.GUI = False

#Initial camera view
bge.Cam = "rotview"

obj = []
matrix = []

scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController() 
own = cont.owner
owner = own

outfile = str(own['file'])

#This list is used in the GUI overlay only
print ("Creating initial model bindings...")
bge.modelDB = {'Tile1': 'DefaultModel', 
'Tile2' : 'DefaultModel',
'Tile3' : 'DefaultModel',
'Tile4' : 'DefaultModel',
'Tile5' : 'DefaultModel',
'Tile6' : 'DefaultModel',
'Tile7' : 'DefaultModel',
'Tile8' : 'DefaultModel',
'Tile9' : 'DefaultModel',
'Tile10' : 'DefaultModel',
'Tile11' : 'DefaultModel',
'Tile12' : 'DefaultModel',
'Tile13' : 'DefaultModel',
'Tile14' : 'DefaultModel',
'Tile15' : 'DefaultModel',
'Tile16' : 'DefaultModel',
'Tile17' : 'DefaultModel',
'Tile18' : 'DefaultModel',
'Tile19' : 'DefaultModel',
'Tile20' : 'DefaultModel',
'Tile21' : 'DefaultModel',
'Tile22' : 'DefaultModel',
'Tile23' : 'DefaultModel',
'Tile24' : 'DefaultModel',
'Tile25' : 'DefaultModel'
}



#This list contains the default models for each table tag (None=DefaultModel)
print ("Setting default model bindings...")
modelMgr = {'C0': "C0Obj", 'C1': None, 'C2': None, 'C3': None, 'C4': None, 'C5': None, 'C6': None, 'C7': None, 'C8': None, 'C9': None, 'C10': None }



FILE = open(outfile,"r")
matrix = FILE.readlines()
FILE.close()

# Here we read new coordinates from 'file'. This 'file' is maintained by an SQL 
# database that is connected to the surface table. We use this file to make sure 
# that the Blender application can run without external libraries (alternatively
# you could import a Python SQL library here to do it directly).
#
#
# [Surface Table] --->  [SQL Database] ---> [sql.dat file]
#
#
# An external script called sql.py takes care of constantly reading database values
# and storing them in sql.dat (which 'file' refers to in Blender)

# Read the database file and put coordinates in 'matrix'
for line in matrix:
	line = line.strip("\n")
	line = line.strip("\r")
	line = line.split(",")
	item = [ int(line[0]), int(line[1]), int(line[2]), int(line[3]), str(line[4])] 
	obj += [item]


# For all tags in 'obj', connect coordinates to appropriate Blender objects
#
# It is useful to use Blender empties as tag references. So create a group of 
# empties called 'C0', 'C1', 'C2', etc. Connect the actual models/objects to these
# empties. This construction allows you to easily remove and add objects to empties 
# in-game, which is useful when you want some more dynamic applications (e.g. remove
# objects, change models on the fly, etc.)
#
#
for i in obj:
	naam = "%s" %(i[4])
	print ("Initial model for %s: %s" %(naam, modelMgr[naam]))
	obj = scene.objects[naam]
	x = (1024- i[1]) / 50
	y = ( i[2]) / 50
	if naam == "C0":
		scene.objects["C0"]['x'] = x
		scene.objects["C0"]['y'] = y
	obj.localPosition = [x,y,.5]
	if modelMgr[naam] == None:
		modelName = "DefaultModel"
	else:
		modelName = modelMgr[naam]
	newObj = scene.addObject(modelName, owner)
	if 'init' in newObj:
		newObj['init'] = 1
	newObj.position = [x,y,.5]
	if 'scale' in newObj:
		print ("   * This object has individual scaling")
		newObj.localScale = [newObj['scale'], newObj['scale'], newObj['scale'] ]
	else:
		newObj.localScale = [1,1,1]	  
	newObj.setParent(obj)
	modelMgr[naam] = newObj
	deg = i[3]
	rad = (3.14 /180) * (deg - 90)
	x = (3.14 / 180) * 90
	obj.worldOrientation=[0,0, -rad]